home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / www / http_log.lha / httplog.c next >
Encoding:
C/C++ Source or Header  |  1995-04-02  |  8.1 KB  |  305 lines

  1. /*
  2. // httplog.c
  3. //
  4. // (c) Armin Obersteiner
  5. //
  6. // Parsing ncsa(cern) httpd logfile
  7. //
  8. // USAGE: httplog [-s <keyword>] [-l] <logfile>
  9. // 
  10. // <logfile>      -  httpd <logfile>
  11. // -l             -  long: country statistics
  12. // -lt            -  long: time statistics
  13. // -s <keyword>   -  search for <keyword>
  14. //
  15. // compiles on:   MaxonC++ (amiga)  
  16. //                gcc (amiga)
  17. //                gcc (bsd)
  18. // (it should actually compile on any platform then :)
  19. //
  20. // it´s made to work on logfiles with american date format and austrian/german time format (24h)
  21. //
  22. // to adapt for other logfiles:
  23. //    the first entry should be the site
  24. //    the second entry is the date in brackets [ ]
  25. //       to adapt this look in lines after "fopen"
  26. //    to include new countries add lines to structure "struct dummy c"
  27. //       don´t forget to increase "country_anz"
  28. //    to adapt time and date do the same with "struct dummy d" / "struct dummy t"
  29. //       don´t forget to increase/decrease "day_anz" :)  / "time_anz"
  30. //    the second entry in these stuctures (dummy) is the string to search for
  31. //       (use spaces or brackets, because it´s more reliable then)
  32. //    the third entry is the string for output
  33. //
  34. // CU Armin :)
  35. //
  36. //    ( Armin.Obersteiner@giga.or.at )
  37. */
  38.  
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <math.h>
  44.  
  45. FILE *f;
  46.  
  47. struct dummy
  48. {
  49.    int i;
  50.    char *ext;
  51.    char *name;
  52. };
  53.    
  54. int country_anz=10;
  55. struct dummy c[10]=
  56. {
  57.    {  0,".at ",   "austria          "  },
  58.    {  0,".de ",   "germany          "  },
  59.    {  0,".fi ",   "finnland         "  },
  60.    {  0,".se ",   "sweden           "  },
  61.    {  0,".no ",   "norway           "  },
  62.    {  0,".edu ",  "usa education    "  },
  63.    {  0,".com ",  "usa commercial   "  },
  64.    {  0,".net ",  "usa network      "  },
  65.    {  0,".gov ",  "usa goverment    "  },
  66.    {  0,".jp ",   "japan            "  }
  67. };
  68.  
  69. int day_anz=7;
  70. struct dummy d[7]=
  71. {
  72.    {  0,"Mon ",      "mon" },
  73.    {  0,"Tue ",      "tue" },
  74.    {  0,"Wed ",      "wed" },
  75.    {  0,"Thu ",      "thu" },
  76.    {  0,"Fri ",      "fri" },
  77.    {  0,"Sat ",      "sat" },
  78.    {  0,"Sun ",      "sun" },
  79. };
  80.  
  81. int time_anz=24;
  82. struct dummy t[24]=
  83. {
  84.    {  0," 00:",      "00"  },
  85.    {  0," 01:",      "01"  },
  86.    {  0," 02:",      "02"  },
  87.    {  0," 03:",      "03"  },
  88.    {  0," 04:",      "04"  },
  89.    {  0," 05:",      "05"  },
  90.    {  0," 06:",      "06"  },
  91.    {  0," 07:",      "07"  },
  92.    {  0," 08:",      "08"  },
  93.    {  0," 09:",      "09"  },
  94.    {  0," 10:",      "10"  },
  95.    {  0," 11:",      "11"  },
  96.    {  0," 12:",      "12"  },
  97.    {  0," 13:",      "13"  },
  98.    {  0," 14:",      "14"  },
  99.    {  0," 15:",      "15"  },
  100.    {  0," 16:",      "16"  },
  101.    {  0," 17:",      "17"  },
  102.    {  0," 18:",      "18"  },
  103.    {  0," 19:",      "19"  },
  104.    {  0," 20:",      "20"  },
  105.    {  0," 21:",      "21"  },
  106.    {  0," 22:",      "22"  },
  107.    {  0," 23:",      "23"  },
  108. };
  109.  
  110. char prg[256];
  111.  
  112. void ende(int n,char *end);
  113.  
  114. void main(int argc, char *argv[])
  115. {
  116.    int anz=0,i=0,j=0,count=0,x;
  117.    double sonst=0;
  118.    int minus_l=0;
  119.    int minus_lt=0;
  120.    int minus_s=0;
  121.  
  122.    double prozent;
  123.  
  124.    char file[256];
  125.    char such[256];
  126.  
  127.    char line[1024],site[1024],back[1024],date[1024];
  128.    /* char method[1024],file[1024],protocol[1024]; */
  129.  
  130.    if(argc<2)
  131.    {
  132.       printf("USAGE: %s [-s <keyword>] [-l[t]] <logfile> \n",argv[0]);
  133.    }
  134.    else
  135.    {
  136.       strcpy(prg,argv[0]);
  137.    
  138.       if(!strcmp(argv[1],"-s"))
  139.       {
  140.          if(argc==4)
  141.          {
  142.             strcpy(such,argv[2]);
  143.             if(!strcmp(argv[2],"-l") || !strcmp(argv[2],"-lt")) ende(1,"wrong arguments");
  144.             if(!strcmp(argv[3],"-l") || !strcmp(argv[3],"-lt")) ende(1,"wrong arguments");
  145.             strcpy(file,argv[3]);
  146.             minus_s=1;
  147.          }
  148.          else if(argc==5)
  149.          {
  150.             strcpy(such,argv[2]);
  151.             if(!strcmp(argv[2],"-l") || !strcmp(argv[2],"-lt")) ende(1,"wrong arguments");
  152.             if(strcmp(argv[3],"-l") && strcmp(argv[3],"-lt")) ende(1,"wrong arguments");
  153.             strcpy(file,argv[4]);            
  154.             minus_s=1;
  155.             if(!strcmp(argv[3],"-l"))
  156.             {
  157.                minus_l=1;
  158.             }
  159.             else minus_lt=1;
  160.    
  161.          }
  162.          else ende(1,"wrong arguments");
  163.       }
  164.       else if( !strcmp(argv[1],"-l") )
  165.       {
  166.          if(argc!=3) ende(1,"wrong arguments");
  167.  
  168.          minus_l=1;
  169.          strcpy(file,argv[2]);
  170.       }
  171.       else if( !strcmp(argv[1],"-lt") )
  172.       {
  173.          if(argc!=3) ende(1,"wrong arguments");
  174.  
  175.          minus_lt=1;
  176.          strcpy(file,argv[2]);
  177.       }
  178.       else strcpy(file,argv[1]);
  179.  
  180.  
  181.       if( (f=fopen(file,"r"))!=NULL )
  182.       {
  183.          char site_old[1024]="";
  184.          char date_old[1024]="";
  185.  
  186.          while( ((fgets(line,1024,f))!=NULL) )
  187.             {
  188.                if(minus_s) strcpy(back,line);
  189.  
  190.                i++;
  191.                strcpy(site,strtok(line,"["));
  192.                strcpy(date,strtok(NULL,"]"));
  193.                /*strcpy(method,strtok(NULL," "));
  194.                strcpy(file,strtok(NULL," "));
  195.                strcpy(protocol,strtok(NULL," \0\n")); */
  196.  
  197.                if(minus_s)
  198.                {
  199.                   if(strstr(back,such)!=NULL)
  200.                   {
  201.                      if(minus_l) for(x=0;x<country_anz;x++) if(strstr(site,c[x].ext)!=NULL) (c[x].i)++;
  202.                      if(minus_lt)
  203.                      {
  204.                         for(x=0;x<day_anz;x++) if(strstr(date,d[x].ext)!=NULL) (d[x].i)++;
  205.                         for(x=0;x<time_anz;x++) if(strstr(date,t[x].ext)!=NULL) (t[x].i)++;
  206.                      }
  207.                      anz++;
  208.                   }
  209.                }
  210.                else
  211.                {
  212.                   if(minus_l) for(x=0;x<country_anz;x++) if(strstr(site,c[x].ext)!=NULL) (c[x].i)++;
  213.                   if(minus_lt)
  214.                   {
  215.                      for(x=0;x<day_anz;x++) if(strstr(date,d[x].ext)!=NULL) (d[x].i)++;
  216.                      for(x=0;x<time_anz;x++) if(strstr(date,t[x].ext)!=NULL) (t[x].i)++;
  217.                   }
  218.                   if(strcmp(site,site_old)!=0) anz++;
  219.                }
  220.  
  221.                /* if(fmod(i,100)==0)printf("*"); */
  222.  
  223.                if(!minus_s) strcpy(site_old,site);
  224.             }
  225.  
  226.          fclose(f);
  227.  
  228.          if(minus_s)
  229.          {
  230.             printf("searching for: %s\n\n",such);
  231.             printf("access: %d/%d  %8.4f %%\n",anz,i,(double)anz*(double)100/(double)i);
  232.          }
  233.          else printf("access: %d/%d\n",anz,i);
  234.          if(minus_l)
  235.          {
  236.             if(i==0) ende(2,"logfile wrong");
  237.  
  238.             printf("\n");
  239.             for(x=0;x<country_anz;x++) if( c[x].i )
  240.                {
  241.                   if(!minus_s)
  242.                   {
  243.                      prozent=(double)c[x].i*(double)100/(double)i;
  244.                   }
  245.                   else
  246.                   {
  247.                      prozent=(double)c[x].i*(double)100/(double)anz;
  248.                   }
  249.                   
  250.                   if(prozent>0.009) printf("%s: %6.2f %%\n",c[x].name,prozent);
  251.                   sonst=sonst+prozent;
  252.                }
  253.             prozent=(double)100-sonst;
  254.  
  255.             if(prozent>0.009) printf("\nothers           : %6.2f %%\n",prozent);
  256.          }
  257.          if(minus_lt)
  258.          {
  259.             if(i==0) ende(2,"logfile wrong");
  260.  
  261.             printf("\n");
  262.  
  263.             for(x=0;x<day_anz;x++) if( d[x].i )
  264.             {
  265.                if(!minus_s)
  266.                {
  267.                   prozent=(double)d[x].i*(double)100/(double)i;
  268.                }
  269.                else
  270.                {
  271.                   prozent=(double)d[x].i*(double)100/(double)anz;
  272.                }
  273.  
  274.                if(prozent>0.009) printf("%s: %6.2f %%\n",d[x].name,prozent);
  275.             }
  276.             printf("\n");
  277.             for(x=0;x<time_anz;x++) if( t[x].i )
  278.             {
  279.                if(!minus_s)
  280.                {
  281.                   prozent=(double)t[x].i*(double)100/(double)i;
  282.                }
  283.                else
  284.                {
  285.                   prozent=(double)t[x].i*(double)100/(double)anz;
  286.                }
  287.  
  288.                if(prozent>0.009) printf("%s: %6.2f %%\n",t[x].name,prozent);
  289.             }
  290.          }
  291.       }
  292.       else ende(5,"can´t open logfile");
  293.  
  294.       ende(0,"");
  295.    }
  296. }
  297.  
  298. void ende(int n,char *end)
  299. {
  300.    if(f) fclose(f);
  301.  
  302.    if(strcmp(end,"")!=0) printf("%s: %s !\n",&prg,end);
  303.    exit(n);
  304. }
  305.